<?
function get_last_merge($branch){
    global $config, $component;
    $cmd = "svn log -v --stop-on-copy ${config[svnroot]}/${config['cprefix']}${component}/$branch";
    exec($cmd, $log);
    if(preg_match_all('/svn\smerge\s\-r\s\d+:(\d+)/', $log = join("\n", $log), $match)){
        rsort($match[1]);
        return array_shift($match[1]);
    } elseif(preg_match('/A\s\/branches\/(.*?)\(from.*\w+:(\d+)\)/', $log, $match)){
        return $match[2];
    } else {
        echo "$log\n";
    }
    return false;
}
function get_branch_name($path = '.'){
    exec("svn info $path", $log, $ret); // https://secure.odesk.com/svn/odeskdev-console/branches/F1222_TEAMROOM_V2/gui_p0
    if($ret) return false;
    if(preg_match('/URL:\s(https:\/\/(?P<root>[\.\w]+)\/svn|svn\+ssh:\/\/[\.\w\@]+)\/(?P<component>[\w\-]+)\/(branches\/(?P<branch>[^\/][\w]+)|trunk)/', join("\n", $log), $match))
        return $match['branch'] ? $match['branch'] : $match[3];
    else return false;
}
function get_components($mode, $qm = true){
    global $config, $branch, $dir;
    if($mode == 'up' || $mode == 'status'){
        $_branch = '';
        $_dir = '';
    } else {
        $_branch = $branch;
        $_dir = $dir . '/';
    }
    $log = array();
    foreach($config['components'] as $cmp=>$paths){
        foreach($paths as $uri => $path){
            do {
                $b = $mode == 'up' || $mode == 'status' ? '' : "${config['svnroot']}/${config['cprefix']}${cmp}/$_branch/$uri";
                $a = "svn $mode $b $_dir$path ";
                switch($mode){
                    case 'status':
                        $a .= '|grep -Ev "^[' . ($qm ? '?' : '') . 'PX]|^$|\.co\.done|__gapp_templates"';
                        break;
                    default:
                        $a .= '2>&1';
                        break;
                }
                if($mode != 'status') echo "\n$a\n\n";
                if($mode == 'co') $a = "[ -f $_dir$path/.co.done ] && echo 'skipped' || $a && touch $_dir$path/.co.done";
                if($mode != 'status') {
                    label("${path}");
                    $out = false;
                    $ln = system($a, $ret);
                } else {
                    $out = '';
                    exec($a, $out, $ret);
                }
                if($ret != 0 && ($mode == 'co' || $mode == 'switch') && $_branch != 'trunk' && preg_match('/^svn.*doesn\'t exist/', $ln)){
                    echo " branch doesnt exists($ret) [$b].\n\t...falling back to trunk ...";
                    $a = "svn $mode ${config['svnroot']}/${config['cprefix']}${cmp}/trunk/$uri $_dir$path";
                    if($mode == 'co') $a = "[ ! -f $_dir$path/.co.done ] && $a && touch $_dir$path/.co.done";
                    $ln = system($a, $ret);
                }
            } while($ret && $keep_trying);
            
            if($mode == 'status' && sizeof((array)$out)) $log = array_merge($log, $out);
        }
    }
    if($mode == 'status') return $log;
}
function checkout($branch, $dir, $mode){
	global $config;
	$dir = strtolower($dir);
	$s = "${config['svnroot']}/${config['cprefix']}console/$branch";
    echo "SVN $mode processing\nbranch: $s\ndir: $dir\n======\n\n";
    label('root');
    $out = system(
        ($mode == 'co' 
            ? "[ ! -f $dir/.co.done ] && (svn co $s/gui_p0 $dir "
            : "(svn $mode $s/gui_p0 $dir "
        ) . ($quite ? '1>/dev/null' : '') . " && touch $dir/.co.done) || echo 'skipped'",
        $ret
    );
    if($ret != 0){
        error("\n====== svn error($ret)[$tmp]\n");
    }
    get_components($mode);
}

function coRC($branch, $path, $component){
    global $config;
    echo "Checking out: $branch\n\n";

    if(!$component) $component = 'console';

    $components = explode(',', $component);
    foreach($components as $c){
        echo $cmd = "svn co ${config['svnroot']}/${config['cprefix']}${c}/$path/$branch $branch/$c" . "\n\n";
        passthru("mkdir -p $branch/$c && $cmd");
        file_put_contents("$branch/$c/.component", $c);
    }
    file_put_contents("$branch/.branch", $branch);
}

